package com.accounting.client.remote.enumeration;

import java.util.List;

import corelib.client.CollectionUtil;
import corelib.client.MathUtil;

public class Currency {
	
	public final static Currency AUD = new Currency("AUD", "澳币",  100);
	public final static Currency RMB = new Currency("RMB", "人民币", 200);
	public final static List<Currency> All = CollectionUtil.newArrayList(AUD, RMB);
	public final static double Rate = 6.0;
	
	private int    type;
	private String name;
	private String cname;
	
	public Currency(String name, String cname, int type){
		this.type = type;
		this.name = name;
		this.cname = cname;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (!(obj instanceof Currency)){
			return false;
		}
		
		Currency currency = (Currency) obj;
		if ( this.name.equals(currency.getName()) 
	      || this.type == currency.getType()
		  || this.cname.equals(currency.getCname())){
			 return true;
		}
		return false;
	}

	public static Currency getInstance(String name, Currency defaultCurrency){
		for (Currency currency : All) {
			if (name.equals(currency.getName())){
				return currency;
			}
		}
		return defaultCurrency;
	}
	
	public static Currency getInstanceByCName(String cname, Currency defaultCurrency){
		for (Currency currency : All) {
			if (cname.equals(currency.getName())){
				return currency;
			}
		}
		return Currency.AUD;
	}
	
	public static double ConvertToAUD(double input, Currency currency){
		if (currency.equals(Currency.AUD)){
			return input;
		}
		
		if (currency.equals(Currency.RMB)){
			return MathUtil.round(input / Rate, 2);
		}
		
		return input;
	}
	
	public static double ConvertToRMB(double input, Currency currency){
		if (currency.equals(Currency.AUD)){
			return MathUtil.round(input * Rate,2);
		}
		if (currency.equals(Currency.RMB)){
			return input;
		}
		
		return input;
	}
}
